﻿using System;

namespace CommonUtils
{
    public class MailHelper
    {
        public MailHelper(string ReceiveAddress, string FromAddress, string FromPassword, string SMTPHost)
        {
            this.ReceiveAddress = ReceiveAddress;
            this.FromAddress = FromAddress;
            this.FromPassword = FromPassword;
            this.SMTPHost = SMTPHost;
        }

        private string ReceiveAddress { get; set; }
        private string FromAddress { get; set; }
        private string SMTPHost { get; set; }
        private string FromPassword { get; set; }

        /// <summary>
        /// 发送告警邮件
        ///
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Content"></param>
        [Obsolete]
        public void SendMail(string Title, string Content)
        {
            if (ReceiveAddress.Contains(","))
            {
                var list = ReceiveAddress.Split(',');
                foreach (var m in list)
                {
                    SendMail(m, Title, Content);
                }
            }
            else
            {
                SendMail(ReceiveAddress, Title, Content);
            }
        }

        [Obsolete]
        private void SendMail(string RecAddress, string Title, string Content)
        {
            try
            {
                System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
                mail.To = RecAddress;
                mail.From = FromAddress;
                mail.Subject = Title;
                mail.BodyFormat = System.Web.Mail.MailFormat.Html;
                mail.Body = Content;

                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份验证  
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mail.From); //邮箱登录账号  
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", FromPassword); //登录密码  
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口  
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密  
                System.Web.Mail.SmtpMail.SmtpServer = SMTPHost;
                System.Web.Mail.SmtpMail.Send(mail);
            }
            catch (Exception ex)
            {
                LogHelper.Error("发送邮件失败", ex);
            }
        }

    }
}
